﻿using Dtos;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class HomeWorkBll : IHomeWorkBll
    {
        private IHomeWorkDal _dal;
        public HomeWorkBll(IHomeWorkDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddHomeWorkAsync(string title, DateTime publishTime, DateTime endTime, DateTime CreateTime, DateTime UpdateTime)
        {
            return await _dal.AddAsync(new Model.HomeWork 
            {
                Title = title,
                PublishTime = publishTime,
                EndTime = endTime,
                CreateTime = CreateTime,
                UpdateTime = UpdateTime
            });
        }

        public async Task<int> DeleteHomeWorkAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return data != null ? await _dal.DeleteAsync(data) : 0;
        }

        public async Task<List<HomeWorkDto>> GetAll()
        {
            var data = await _dal.GetAll();
            return data.Select(hw => new HomeWorkDto
            {
                Id = hw.Id,
                Title = hw.Title,
                PublishTime = hw.PublishTime,
                EndTime = hw.EndTime,
                CreateTime = hw.CreateTime,
                UpdateTime = hw.UpdateTime
            }).ToList();
        }

        public async Task<HomeWorkDto> GetHomeWorkByIdAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return new HomeWorkDto 
            {
                Id = data.Id,
                Title = data.Title,
                PublishTime = data.PublishTime,
                EndTime  = data.EndTime,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<List<HomeWorkDto>> GetHomeWorkByNameAsync(string title)
        {
            var data = await _dal.GetDataByChoose(hw => hw.Title.Contains(title));
            return data == null ? null :
                data.Select(hw => new HomeWorkDto
                {
                    Id = hw.Id,
                    Title = hw.Title,
                    PublishTime = hw.PublishTime,
                    EndTime = hw.EndTime,
                    CreateTime = hw.CreateTime,
                    UpdateTime = hw.UpdateTime
                }).ToList();
        }

        public async Task<int> UpdateHomeWorkAsync(Guid id, string title, DateTime publishTime, DateTime endTime,DateTime updateTime)
        {
            var data = await _dal.GetById(id);
            if(data == null)
                return 0;
            data.Title = title;
            data.PublishTime = publishTime;
            data.EndTime = endTime;
            data.UpdateTime = updateTime;
            return await _dal.UpdateAsync(data);
        }
    }
}
